package logic

import (
	"bw-renting/common"
	"bw-renting/model/mysql"
	"bw-renting/order-rpc/internal/svc"
	"bw-renting/order-rpc/order"
	"context"
	"errors"
	"log"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// Todo：创建订单
func (l *CreateOrderLogic) CreateOrder(in *order.CreateOrderReq) (*order.OrderRes, error) {
	// todo: add your logic here and delete this line
	UserId := in.UserId
	user := mysql.GetUser(UserId)
	if user.ID == 0 {
		return &order.OrderRes{OrderId: 0}, errors.New("用户信息不存在")
	}

	HouseOrder := mysql.HouseOrder{
		UserId:    UserId,
		HouseId:   in.HouseId,
		Title:     in.Title,
		OrderSn:   common.GenerateOrderNumber(),
		StartDate: in.StartDate,
		EndDate:   in.EndDate,
		Price:     float64(in.Price),
		Status:    0,
	}
	createOrderResult := mysql.CreateOrder(HouseOrder)
	log.Println(createOrderResult)
	return &order.OrderRes{
		OrderId:     int64(createOrderResult.ID),
		OrderNumber: createOrderResult.OrderSn,
	}, nil
}
